/**
 * Dynamic sidebar modification.
 *
 * Depends on:
 *		jquery
 *		tag_tree.js
 */
$(document).ready(function() {
	var MOTHER_LIST = $('#navigation-list');
	var BOOK_LIST = $('#tree-navigation');

	var ICON_PAGE = '<i class="icon-list-alt pull-right "></i> ';
	var ICON_TEST = '<i class="icon-pencil pull-right"></i> ';
	var ICON_LEAF = '<i class="icon-filler"></i> ';
	var ICON_ROOT_BRANCH = '<i class="icon-book pull-right"></i> ';
	var ICON_ROOT_LEAF = ICON_LEAF;
	var ICON_ROOT_COLLAPSED = '<i class="icon-chevron-right"></i> ';
	var ICON_ROOT_EXPANDED = '<i class="icon-chevron-down"></i> ';
	var ICON_BRANCH_COLLAPSED = ICON_ROOT_COLLAPSED;
	var ICON_BRANCH_EXPANDED = ICON_ROOT_EXPANDED;

	var startCollapsed = true;
	var enableCollapse = false;
	var listType = "ul";
	var treeTagger = new TreeTagger(BOOK_LIST, startCollapsed, enableCollapse, listType);

	var activeLi = null;

	//Collapse the book - close all sub-lists
	BOOK_LIST.find(listType).hide();

	//Add icons to the book root, and treat as all collapsed
	BOOK_LIST.children('li').each(function() {
		if($(this).hasClass('branch')) {
			$(this).children('a').prepend(ICON_ROOT_BRANCH);
			$(this).children('a').prepend(ICON_ROOT_COLLAPSED);
		} else {
			$(this).children('a').prepend(ICON_ROOT_LEAF);

			if($(this).hasClass('test')) {
				$(this).children('a').prepend(ICON_TEST);
			} else {
				$(this).children('a').prepend(ICON_PAGE);
			}
		}		
	});

	//Add icons to all sub-book-root items
	BOOK_LIST.children('li').children(listType).find('li').each(function() {
		if($(this).hasClass('test')) {
			$(this).children('a').prepend(ICON_TEST);
		} else {
			$(this).children('a').prepend(ICON_PAGE);
		}

		if($(this).hasClass('branch')) {
			$(this).children('a').prepend(ICON_BRANCH_COLLAPSED);
		} else {
			$(this).children('a').prepend(ICON_LEAF);
		}
	});

	//If a link is visited right now, mark it active and store the li
	MOTHER_LIST.find('a').each(function() {
		var link = $(this).attr('href');
		if(link == location.pathname) {
			activeLi = $(this).closest('li');
			activeLi.addClass('active');
			return false; //Break each - NOTE: disables multiple selection
		}
	});

	//Show parents and children of the active list item
	if(activeLi != null && !activeLi.parent().is(MOTHER_LIST)) {
		//Show sublist of active list item
		if(activeLi.hasClass('branch')) {
			activeLi.children('a').children('i').first().remove();
			activeLi.children('a').prepend(ICON_ROOT_EXPANDED);
			activeLi.children(listType).show();
		}

		var parent = activeLi.parent();
		if(!parent.is(BOOK_LIST)) {
			while(!parent.hasClass('root')) {
				if(parent.is('li')) {
					parent.children('a').children('i').first().remove();
					parent.children('a').prepend(ICON_ROOT_EXPANDED);
				} else {
					parent.show();
				}

				parent = parent.parent();
			}
		}

		//Parent is now a root page
		if(parent.hasClass('branch')) {
			parent.children('a').children('i').first().remove();
			parent.children('a').prepend(ICON_ROOT_EXPANDED);
		}
	}

	//Add bootstrap css classes to all list-tags
	BOOK_LIST.find(listType).addClass('nav nav-list');

	//Add indent in a-tags, ie child nodes in the tree
	BOOK_LIST.find(listType).each(function() {
		lis = $(this).find('li');
		lis.children('a').prepend('<span class="indent"></span>');
	});
});